// pages/my/balance/moneydetail/moneydetail.js
var app = getApp();
var pageoffset, pagenum;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    details: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      balance: app.globalData.balance
    });

    pageoffset = 1;
    pagenum = 20;
    wx.showLoading({
      title: '',
    });
    this.loadDetail(pageoffset);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log('bottom');
    pageoffset++;
    this.loadDetail(pageoffset);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  updatePwd: function () {
    wx.navigateTo({
      url: '../password/password?target=update',
    });
  },
  rechargeMoney: function () {
    wx.redirectTo({
      url: '../balance',
    });
  },
  loadDetail: function (offset) {
    var url = '/yuanmai_we/user/userinfo/recharge/' + offset + '/' + pagenum;
    app.loadDataFromServer(url, this.loadDetailCallBack, {}, 'get', true);
  },
  loadDetailCallBack: function (res) {
    wx.hideLoading();
    console.log(res);
    if(res.info.length <= 0){
      pageoffset--;
      return;
    }
    var details = [];
    if (pageoffset > 1) {
      details = this.data.details;
    }
    for (var i = 0; i < res.info.length; i++) {
      res.info[i].type = parseInt(res.info[i].num) > 0 ? '1':'-1';
      res.info[i].num = Math.abs(res.info[i].num);
      details.push(res.info[i]);
    }
    this.setData({
      details: details
    });
  }
})